package com.cn.qgx428.lazydiary.Presenter

import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.cn.qgx428.lazydiary.APP
import com.cn.qgx428.lazydiary.Presenter.contract.UserContract
import com.cn.qgx428.lazydiary.Utils.ToastHelper
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.Imageloader
import com.cn.qgx428.lazydiary.network.NetworkManager
import com.cn.qgx428.lazydiary.network.bean.CommonResponse
import com.cn.qgx428.lazydiary.network.bean.User
import com.cn.qgx428.lazydiary.network.bean.UserInfoResponse
import rx.Observable
import rx.Observer
import rx.Scheduler
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
 * Created by qinguanxiong on 2018/1/3.
 */
class UserPresenter {
    companion object {
        /**
         * 注册
         */
        fun register(phone:String , pwd:String, nickname:String , contruct: UserContract.IUserRegister){
            NetworkManager.getInstance().getUserService()
                    .register(phone , pwd, nickname)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : Observer<CommonResponse>{
                        override fun onCompleted() {

                        }

                        override fun onNext(t: CommonResponse?) {
                            if (t?.code == "1")
                                contruct.onRegisterFaild(t.msg)
                            else{
                                contruct.onRegister(t!!.msg)
                                var msg = LAMessege();
                                msg.arg1 = phone
                                msg.arg2 = pwd
                                LManager.newInstance().trigger(LAction.ACTION_REGISTER ,msg)
                            }

                        }

                        override fun onError(e: Throwable?) {
                            Log.d("TTT",e?.message)
                        }

                    })
        }

        /**
         * 登陆
         */
        fun login(phone: String, pwd: String, contruct: UserContract.IUserLogin){
            NetworkManager.getInstance().getUserService()
                    .login(phone,pwd)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : Observer<UserInfoResponse>{
                        override fun onError(e: Throwable?) {
                            ToastHelper.toast("登陆错误 :" + e.toString())
                        }

                        override fun onNext(t: UserInfoResponse?) {
                            if (t!!.code.equals("-2")){
                                contruct.onLoginFailed(t.msg)
                            } else{
                                contruct.onLogin(t.user,t.msg)
                            }
                        }


                        override fun onCompleted() {
                        }

                    })
        }

        /**
         * 获取用户信息
         */
        fun getUserInfo(id: Int, listener: (user: User)-> Unit){
            NetworkManager.getInstance()
                    .getUserService()
                    .getUserInfo(id)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : Observer<User>{
                        override fun onNext(t: User?) {
                            listener(t!!)
                        }

                        override fun onCompleted() {
                        }

                        override fun onError(e: Throwable?) {

                        }

                    })
        }


        /**
         * 更新用户信息
         */
        fun updateUserInfo(id: Int,nickname: String, gender: String, imageName: String = "", filePath: String = "", listener:(msg: String)-> Unit){
            if (!TextUtils.isEmpty(imageName)){
                Imageloader.upload(filePath, imageName,{
                  NetworkManager.getInstance().getUserService()
                          .updateUserInfo(id, nickname, gender, imageName)
                          .subscribeOn(Schedulers.newThread())
                          .observeOn(AndroidSchedulers.mainThread())
                          .subscribe(object : Observer<CommonResponse>{
                              override fun onError(e: Throwable?) {
                                  ToastHelper.toast("更新用户信息错误 :" + e.toString())
                              }

                              override fun onNext(t: CommonResponse?) {
                                  listener(t!!.msg)
                              }

                              override fun onCompleted() {
                              }

                          })

                })
            }

            else{
                NetworkManager.getInstance().getUserService()
                        .updateUserInfo(id, nickname, gender, imageName)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : Observer<CommonResponse>{
                            override fun onError(e: Throwable?) {
                                ToastHelper.toast("更新用户信息错误 :" + e.toString())
                            }

                            override fun onNext(t: CommonResponse?) {
                                listener(t!!.msg)
                            }

                            override fun onCompleted() {
                            }

                        })
            }
        }

        /**
         * 重制密码
         */
        fun resetPwd(id: Int, originPwd:String, newPwd:String, listener:(res: CommonResponse) -> Unit){
            NetworkManager.getInstance().getUserService()
                    .resetPwd(id, originPwd, newPwd)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : Observer<CommonResponse>{
                        override fun onError(e: Throwable?) {
                            ToastHelper.toast("服务器错误")
                            Log.d("User", "reset error !!  "  + e!!.message)
                        }

                        override fun onNext(t: CommonResponse?) {
                            listener(t!!)
                        }

                        override fun onCompleted() {
                        }

                    })
        }
    }
}